package control;
import java.util.*;

import model.*;
import db.*;

/*This is the Vehicle controller.
 * Its purpose is to give instructions towards manipulating the data concerning vehicles, including adding, updating, removing
 * or simply returning a vehicle for inspection. 
 */

public class VehicleCtrl 
{
	private DBVehicle dbVehicle = new DBVehicle();
	
	public VehicleCtrl()
	{
		
	}
	
	// This method returns all vehicles in the form of an ArrayList.
	
	public ArrayList<Vehicle> listAllVehicle()
	{
		ArrayList<Vehicle> allVehicles;
		allVehicles = dbVehicle.getAllVehicles(false);
		return allVehicles;
	}
	
	public ArrayList<Vehicle> getAllVehicles()
	{
		return dbVehicle.getAllVehicles(false);
	}
	
	// This method searches for a vehicle by their license plate, and returns the result.
	
	public Vehicle getVehicleByLicensePlate(String licensePlate)
	{
		return dbVehicle.getVehicleByLicensePlate(licensePlate,true);
	}
	
	//This method inserts a new vehicle in the database.
	
	public void createVehicle(String licensePlate, String type, String year, String color, String factoryNr) throws Exception
	{
		Vehicle toInsert = new Vehicle();
		toInsert.setLicensePlate(licensePlate);
		toInsert.setType(type);
		toInsert.setColor(color);
		toInsert.setFactoryNr(factoryNr);
		toInsert.setYear(year);
		dbVehicle.insertVehicle(toInsert);
	}
	
	// This method updates the vehicle, the parameters being the fields that will be modified (except licensePlate, which
	// is used to search for the vehicle in the dabatase).
	
	public int updateVehicle(String licensePlate, String type, String year, String color, String factoryNr)
	{
		Vehicle toUpdate = dbVehicle.getVehicleByLicensePlate(licensePlate,true);
		toUpdate.setLicensePlate(licensePlate);
		toUpdate.setColor(color);
		toUpdate.setFactoryNr(factoryNr);
		toUpdate.setType(type);
		toUpdate.setYear(year);
		return dbVehicle.updateVehicle(toUpdate);
	}
	
	// This method removes a vehicle from the database.
	
	public boolean deleteVehicle(String licensePlate)
	{
		return dbVehicle.deleteVehicle(licensePlate);
	}

}
